package com.kb.erp.service.impl;


import cn.hutool.http.HttpUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kb.erp.domain.DYYTokenEntity;
import com.kb.erp.mapper.mysql.DYYMapper;
import com.kb.erp.service.DYYService;
import com.kb.erp.util.GuavaCacheUtil;
import com.kb.erp.util.MessageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 业务字段 服务层实现
 *
 * @author 黄忠开
 */
@Service
@DS("master")
public class DYYServiceImpl implements DYYService {
    private String DYYTOKENKEY = "dyyToken";

    @Autowired
    private DYYMapper dyyMapper;


    @Override
    public String getDYYToken() {
        String dyytokenkey = GuavaCacheUtil.getStr(DYYTOKENKEY);
        if (StringUtils.isNotBlank(dyytokenkey)){
            return dyytokenkey;
        }
        LambdaQueryWrapper<DYYTokenEntity> query = new LambdaQueryWrapper<>();
        query.eq(DYYTokenEntity::getKeyColumn, "token");
        DYYTokenEntity dyyTokenEntity = dyyMapper.selectOne(query);
        return dyyTokenEntity.getValueColumn();
    }

    @Override
    public void delKey() {
        GuavaCacheUtil.delStr(DYYTOKENKEY);
    }

    @Override
    public String updateDYYToken() {
        try {
            LambdaQueryWrapper<DYYTokenEntity> query = new LambdaQueryWrapper<>();
            query.eq(DYYTokenEntity::getKeyColumn, "token");
            DYYTokenEntity dyyTokenEntity = dyyMapper.selectOne(query);

            String token = this.getToken();
            DYYTokenEntity addEntity = new DYYTokenEntity();
            addEntity.setId(dyyTokenEntity.getId());
            addEntity.setValueColumn(token);
            addEntity.setCreateDate(LocalDateTime.now());
            dyyMapper.updateById(addEntity);

            //加到缓存
            GuavaCacheUtil.setStr(DYYTOKENKEY,token);
        } catch (Exception e) {
            MessageUtil.sentMessageToAssistant("22361|23988", e.getMessage());
            throw new RuntimeException(e);
        }

        return null;
    }

    private String getToken() {
        //获取token
        String tokenStr = HttpUtil.get("https://qwif.do1.com.cn/qwcgi/portal/api/getToken.do?" + "developerId=qw0d636f58fa264d15a192b3f4946dddd8&developerKey=NWQyODI3OTktOTIwMS00ODYwLWI3YWItZDk4ZDA2ZDg1YzA4");
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = null;
        String token = null;
        try {
            jsonNode = objectMapper.readTree(tokenStr);
            JsonNode dataNode = jsonNode.get("data");
            token = dataNode.get("token").asText();
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return token;
    }
}